#!/cygdrive/c/Python27/python

import sys, os
from twisted.internet import wxreactor
wxreactor.install ()
from twisted.internet import reactor, defer
from twisted.python import log
import wx
# not sure why i can't go wx.grid, but here we are
from wx import grid

ID_EXIT  = 101
ID_DOWNLOAD = 102

class Frame (wx.Frame):
    def __init__ (self, parent, id, title):
        wx.Frame.__init__ (self, parent, id, title)

        menu = wx.Menu()
        menu.Append(ID_EXIT, "E&xit", "Terminate the program")
        menu.Append(ID_DOWNLOAD, "&Download", "Download stuff")
        menuBar = wx.MenuBar()
        menuBar.Append(menu, "&File")
        self.SetMenuBar(menuBar)
        wx.EVT_MENU(self, ID_EXIT,  self.DoExit)
        wx.EVT_MENU(self, ID_DOWNLOAD,  self.DoDownload)
        
        g = grid.Grid(self)
        g.CreateGrid(1,1)
        
        # make sure reactor.stop() is used to stop event loop:
        # wx.EVT_CLOSE(self, lambda evt: reactor.stop())


        btn_dlg = wx.Button (self, -1, "Press", size=(240, 180))
        btn_dlg.Bind (wx.EVT_BUTTON, self.click)

    def DoExit (self, event):
        pass

    def DoDownload (self, event):
        pass

    def click (self, event):
        
        reactor.callLater(10,self.tester)
        d = defer.Deferred ()
        self.prepare (d)

    def tester(self):
        print("yay")

    def prepare (self, d):
        """
        uncomment reactor.callLater or d.callback to test
        """
        d.addCallback (self.show)
        reactor.callLater (1.0, d.callback, 1) # this will fail
        #d.callback (1) # this will work
        
    def show (self, val):
        for i in range (20):
            reactor.callLater (i*0.2, self.dosth, i) # these will be printed _after_ dialog has been closed
        dlg = wx.MessageDialog (self, "I am a dialog", "Dialog", style=wx.OK|wx.CANCEL)
        ruckgabe = dlg.ShowModal ()
        dlg.Destroy ()
        
        if ruckgabe == wx.ID_OK:
            print("okay")
        else:
            print("not okay")

    def dosth (self, idx):
        print("doing something nr" + str(idx) )

class App (wx.App):
    def OnInit (self):
        self.frame = Frame (None, -1, "Test")
        self.frame.Show (1)
        self.SetTopWindow (self.frame)
        return True

if __name__ == "__main__":
    app = App (1)
    reactor.registerWxApp (app)
    reactor.run ()